"""
追踪眼+眨眼+表情切换模块
配置：
    眼部配置
    表情配置
追踪眼功能实现：
    EyeTracker持续跟踪鼠标并刷新坐标
    eye_tracker_component 持续绘制瞳孔
眨眼功能实现:
    blink_transform控制眨眼
    可以用switch_blink控制其开关
表情切换模块:
    change_expression函数

"""

# 定义眼部配置（可以在不同角色间复用）
define EYE_CONFIG = {
    "default": {
        "eye_relative_x": 0,
        "eye_relative_y": 0,
        "max_offset_x": 2,
        "max_offset_y": 0.8,
        "sensitivity": 0.1,
        "smoothness": 0.1
    },
}
#表情切换开关
default switch_blink = True
# 图片定义
image character_eye_iris = "images/lan_eye_iris.png"
image character_body = "images/lan_body.png"
image character_eye_white = "images/lan_eye_white.png"
image character_eye_close = "images/lan_eye_close.png"

# 表情图片 - 添加不同的表情
image character_happy = "images/lan_happy.png"
image character_shy = "images/lan_shy.png"
image character_angry = "images/lan_angry.png"
image character_surprised = "images/lan_surprised.png"
image character_puzzled = "images/lan_puzzled.png"
image character_exin = "images/lan_exin.png"

# 表情配置
define EXPRESSION_CONFIG = {
    "default": "character_body",
    "happy": "character_happy",
    "shy": "character_shy", 
    "angry": "character_angry",
    "surprised": "character_surprised",
    "puzzled":"character_puzzled",
    "exin":"character_exin"
}

# 定义眼部跟踪变换
transform eye_tracking_transform(eye_config, character_xalign=0.5, character_yalign=0.5):
    function EyeTracker(eye_config, character_xalign, character_yalign)
    repeat

# 眨眼变换
transform blink_transform:
    alpha 0.0
    pause renpy.random.uniform(1.0, 2.0)
    alpha 1.0
    pause 0.1
    alpha 0.0
    repeat

init python:
    class EyeTracker(object):
        def __init__(self, eye_config, character_xalign, character_yalign):
            self.eye_config = eye_config
            self.character_xalign = character_xalign
            self.character_yalign = character_yalign
            self.current_offset_x = 0.0
            self.current_offset_y = 0.0
            
        def __call__(self, trans, st, at):
            """实现眼部数据追踪"""
            mouse_x, mouse_y = renpy.get_mouse_pos()
            
            char_screen_x = 1920 * self.character_xalign
            char_screen_y = 1080 * self.character_yalign
            
            eye_base_x = char_screen_x + self.eye_config["eye_relative_x"]
            eye_base_y = char_screen_y + self.eye_config["eye_relative_y"]
            
            target_x = (mouse_x - eye_base_x) * self.eye_config["sensitivity"]
            target_y = (mouse_y - eye_base_y) * self.eye_config["sensitivity"]
            
            self.current_offset_x += (target_x - self.current_offset_x) * self.eye_config["smoothness"]
            self.current_offset_y += (target_y - self.current_offset_y) * self.eye_config["smoothness"]
            
            self.current_offset_x = max(-self.eye_config["max_offset_x"], min(self.eye_config["max_offset_x"], self.current_offset_x))
            self.current_offset_y = max(-self.eye_config["max_offset_y"], min(self.eye_config["max_offset_y"], self.current_offset_y))
            
            trans.xoffset = self.eye_config["eye_relative_x"] + self.current_offset_x
            trans.yoffset = self.eye_config["eye_relative_y"] + self.current_offset_y
            
            return 0.016

    # 表情管理函数
    def change_expression(expression_name="default"):
        """
        切换角色表情
        expression_name: 表情名称，对应EXPRESSION_CONFIG中的键
        """
        if expression_name in EXPRESSION_CONFIG:
            renpy.show_screen("character_screen", 
                            character_name='蓝',
                            body_image=EXPRESSION_CONFIG[expression_name],
                            eye_white_image="character_eye_white",
                            eye_iris_image="character_eye_iris")
            return True
        else:
            renpy.notify("未知的表情: " + expression_name)
            return False

# 简化的眼部跟踪Screen组件
screen eye_tracker_component(character_id="default", character_xalign=0.5, character_yalign=0.5, 
                            eye_iris_image="character_eye_iris"):
    
    default eye_config = EYE_CONFIG.get(character_id, EYE_CONFIG["default"])
    
    # 显示虹膜（应用ATL变换）
    add eye_iris_image:
        xalign character_xalign
        yalign character_yalign
        ysize 800
        fit "contain"
        at eye_tracking_transform(eye_config, character_xalign, character_yalign)

# 眨眼组件
screen blink_component(character_xalign=0.5, character_yalign=0.5):
    """
    眨眼组件 - 作为最上层覆盖
    """
    if switch_blink:
        add "character_eye_close":
            xalign character_xalign
            yalign character_yalign
            ysize 800
            fit "contain"
            
            at blink_transform



# 角色管理Screen - 支持表情切换
screen character_screen(character_name='角色', character_xalign=0.35, character_yalign=0.5, 
                    body_image="character_body", eye_white_image="character_eye_white",
                    eye_iris_image="character_eye_iris", eye_config_id="default"):
    """
    完整的角色Screen - 支持动态表情切换
    """
    
    zorder 50
    
    # 显示眼白（第一层）
    add eye_white_image:
        xalign character_xalign
        yalign character_yalign
        ysize 800
        fit "contain"
    
    # 集成眼部跟踪组件（第二层 - 瞳孔）
    use eye_tracker_component(character_id=eye_config_id, 
                            character_xalign=character_xalign, 
                            character_yalign=character_yalign,
                            eye_iris_image=eye_iris_image)
    
    # 显示身体/表情（第三层）
    add body_image:
        xalign character_xalign
        yalign character_yalign
        ysize 800
        fit "contain"
    
    # 眨眼组件（最上层 - 覆盖在其他眼部组件之上）
    use blink_component(character_xalign=character_xalign, character_yalign=character_yalign)

label character_show:
    # 显示角色（默认表情）
    show screen character_screen(character_name='蓝')
    
    "女主角蓝出现了！"
    "眼部跟踪测试..."
    
    "嘿嘿！现在角色会自然眨眼了！"
    "切换表情看看效果"
    
    # 测试表情切换
    $ change_expression("happy")
    "开心！"
    
    $ change_expression("shy")
    "害羞..."
    $ switch_blink = False
    $ change_expression("exin")
    "咦......"
    $ switch_blink = True

    $ change_expression("angry")
    "生气！"
    $ change_expression("puzzled")
    "疑惑"
    $ change_expression("surprised")
    "惊讶！"
    
    

    
    $ change_expression("default")
    "一般表情啦"
    
    # 测试移动角色
    show screen character_screen(character_name='蓝', character_xalign=0.3)
    
    "移到左侧，眼睛跟踪和眨眼效果都还在！"
    
    "测试结束"